<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis6.0-lettuce%E6%94%AF%E6%8C%81%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%BC%93%E5%AD%98/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">307 字 ~2分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h1 id="lettuce-已支持-redis6-客户端缓存">Lettuce 已支持 Redis6 客户端缓存</h1>

<p><strong>简介：</strong> 在 <strong>redis 6.0</strong> 版本中，已经默认支持了客户端缓存功能，Java 中主流的连接客户端 <strong>lettuce</strong> 在最新的快照版本 (6.0.0.BUILD-SNAPSHOT) 已经提供支持。 下边就通过代码来体验一下客户端缓存的神奇功能。</p>

<h2 id="redis-客户端缓存">Redis 客户端缓存</h2>

<p>缓存的解决方案一般有两种：</p>

<p>【L1】 内存缓存（如 Caffeine、Ehcache） —— 速度快，进程内可用，但重启缓存丢失，出现缓存雪崩的问题。</p>

<p>【L2】集中式缓存（如 Redis）—— 可同时为多节点提供服务，但高并发下，带宽成为瓶颈。</p>

<p>业内有很多开源框架来解决以上问题，既能有 L1 速度，并且拥有 L2 集群态。如下</p>

<p><a href="https://gitee.com/ld/J2Cache?spm=a2c6h.12873639.article-detail.3.28fb765fDydFkx">J2Cache 两级缓存框架</a></p>

<p><a href="https://gitee.com/jd-platform-opensource/hotkey?spm=a2c6h.12873639.article-detail.4.28fb765fDydFkx">hotkey 热点数据实时同步</a></p>

<p>在 <strong>redis 6.0</strong> 版本中，已经默认支持了客户端缓存功能，Java 中主流的连接客户端 <strong>lettuce</strong> 在最新的快照版本 (6.0.0.BUILD-SNAPSHOT) 已经提供支持。 下边就通过代码来体验一下客户端缓存的神奇功能。</p>

<h2 id="redis-6-0-安装">Redis 6.0 安装</h2>

<p>安装 <code>redis 6</code>,这里通过 Docker 安装命令如下</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">docker run --name redis6 -p 6379:6379 --restart<span style="color:#ce5c00;font-weight:bold">=</span>always -d redis:6.0.6</code></pre></div>
<h2 id="jar-依赖">Jar 依赖</h2>

<p>注意： 这里使用 lettuce 客户端，注意当前使用 6.0 的快照版本 ，需要在 pom 增加 lettuce 快照仓库</p>

<ul>
<li><p>1.lettuce 6.0 快照依赖</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-xml" data-lang="xml"><span style="color:#204a87;font-weight:bold">&lt;dependency</span><span style="color:#204a87;font-weight:bold">&gt;</span>
<span style="color:#204a87;font-weight:bold">&lt;groupId</span><span style="color:#204a87;font-weight:bold">&gt;</span>io.lettuce<span style="color:#204a87;font-weight:bold">&lt;/groupId&gt;</span>
<span style="color:#204a87;font-weight:bold">&lt;artifactId</span><span style="color:#204a87;font-weight:bold">&gt;</span>lettuce-core<span style="color:#204a87;font-weight:bold">&lt;/artifactId&gt;</span>
<span style="color:#204a87;font-weight:bold">&lt;version</span><span style="color:#204a87;font-weight:bold">&gt;</span>6.0.0.BUILD-SNAPSHOT<span style="color:#204a87;font-weight:bold">&lt;/version&gt;</span>
<span style="color:#204a87;font-weight:bold">&lt;/dependency&gt;</span></code></pre></div></li>

<li><ol>
<li><p>配置快照仓库</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-xml" data-lang="xml"><span style="color:#204a87;font-weight:bold">&lt;repositories</span><span style="color:#204a87;font-weight:bold">&gt;</span>
<span style="color:#204a87;font-weight:bold">&lt;repository</span><span style="color:#204a87;font-weight:bold">&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;id</span><span style="color:#204a87;font-weight:bold">&gt;</span>sonatype-snapshots<span style="color:#204a87;font-weight:bold">&lt;/id&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;name</span><span style="color:#204a87;font-weight:bold">&gt;</span>Sonatype Snapshot Repository<span style="color:#204a87;font-weight:bold">&lt;/name&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;url</span><span style="color:#204a87;font-weight:bold">&gt;</span>https://oss.sonatype.org/content/repositories/snapshots/<span style="color:#204a87;font-weight:bold">&lt;/url&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;snapshots</span><span style="color:#204a87;font-weight:bold">&gt;</span>
        <span style="color:#204a87;font-weight:bold">&lt;enabled</span><span style="color:#204a87;font-weight:bold">&gt;</span>true<span style="color:#204a87;font-weight:bold">&lt;/enabled&gt;</span>
    <span style="color:#204a87;font-weight:bold">&lt;/snapshots&gt;</span>
<span style="color:#204a87;font-weight:bold">&lt;/repository&gt;</span>
<span style="color:#204a87;font-weight:bold">&lt;/repositories&gt;</span></code></pre></div></li>
</ol></li>
</ul>

<h2 id="代码操作">代码操作</h2>

<ul>
<li><p>使用 lettuce 连接 redis ，并循环查看 k1 的值</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#8f5902;font-style:italic">// &lt;1&gt; 创建单机连接的连接信息
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">RedisURI</span> <span style="color:#000">redisUri</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">RedisURI</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">builder</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span>                    <span style="color:#8f5902;font-style:italic">//
</span><span style="color:#8f5902;font-style:italic"></span>    <span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">withHost</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;127.0.0.1&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span>
    <span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">withPort</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">6379</span><span style="color:#ce5c00;font-weight:bold">)</span>
    <span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">build</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">RedisClient</span> <span style="color:#000">redisClient</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">RedisClient</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">create</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">redisUri</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">StatefulRedisConnection</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">otherParty</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">redisClient</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">connect</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">RedisCommands</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">commands</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">otherParty</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">sync</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">StatefulRedisConnection</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">connection</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">redisClient</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">connect</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">// &lt;2&gt; 创建缓存访问器
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">Map</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">clientCache</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">ConcurrentHashMap</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">//map 自动保存所有操作key的 key=value
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">CacheFrontend</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">frontend</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">ClientSideCaching</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">enable</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">CacheAccessor</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">forMap</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">clientCache</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">connection</span><span style="color:#ce5c00;font-weight:bold">,</span>
    <span style="color:#000">TrackingArgs</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">Builder</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">enabled</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>

<span style="color:#8f5902;font-style:italic">// &lt;3&gt; 客户端正常写入测试数据 k1 v1
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">String</span> <span style="color:#000">key</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06">&#34;k1&#34;</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">commands</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">set</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">key</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;v1&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">// &lt;4&gt; 循环读取
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">while</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">true</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
<span style="color:#8f5902;font-style:italic">// &lt;4.1&gt; 缓存访问器中的值，查看是否和 Redis 服务端同步
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">String</span> <span style="color:#000">cachedValue</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">frontend</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">get</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">key</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;当前 k1 的值为:---&gt;&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">cachedValue</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#000">Thread</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">sleep</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">3000</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div></li>

<li><p>redis-cli 客户端同时操作 k1 修改 k1 的值</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">./redis-cli -h 127.0.0.1 -p <span style="color:#0000cf;font-weight:bold">6379</span>

&gt; <span style="color:#204a87">set</span> k1 v2</code></pre></div></li>

<li><p>注意查看 控制台日志</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text">...
当前 k1 的值为:---&gt;v1
当前 k1 的值为:---&gt;v1
当前 k1 的值为:---&gt;v1
当前 k1 的值为:---&gt;v2
当前 k1 的值为:---&gt;v2
当前 k1 的值为:---&gt;v2
....</code></pre></div></li>
</ul>

<p>如上： k1 的值在其他客户端（redis-cli）修改，lettuce 客户端确实感知到了数据变化。 但 lettuce 到底 <code>CacheFrontend.get</code> 到底有没有查询 redis 呢？</p>

<p>我们可以通过以下监控看下客户端具体的操作细节</p>

<h2 id="监控">监控</h2>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">./redis-cli -h 127.0.0.1 -p <span style="color:#0000cf;font-weight:bold">6379</span>

&gt; MONITOR
OK
1595922453.165088 <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">0</span> 172.16.1.96:57482<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#4e9a06">&#34;SET&#34;</span> <span style="color:#4e9a06">&#34;k1&#34;</span> <span style="color:#4e9a06">&#34;v1&#34;</span>   <span style="color:#8f5902;font-style:italic"># 对应 &lt;3&gt; 写入测试数据</span>
1595922453.168238 <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">0</span> 172.16.1.96:57483<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#4e9a06">&#34;GET&#34;</span> <span style="color:#4e9a06">&#34;k1&#34;</span>        <span style="color:#8f5902;font-style:italic"># &lt;4.1&gt; 缓存访问器中的值，由于第一次查询为空需要穿透去查询 redis-server</span>
1595922466.525942 <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">0</span> 172.16.1.96:57498<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#4e9a06">&#34;COMMAND&#34;</span>         <span style="color:#8f5902;font-style:italic"># 其他客户端 redis-cli 接入 提醒</span>
1595922472.046488 <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">0</span> 172.16.1.96:57498<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#4e9a06">&#34;set&#34;</span> <span style="color:#4e9a06">&#34;k1&#34;</span> <span style="color:#4e9a06">&#34;v2&#34;</span>   <span style="color:#8f5902;font-style:italic"># 其他客户端 操作 k1</span>
1595922474.208214 <span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#0000cf;font-weight:bold">0</span> 172.16.1.96:57483<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#4e9a06">&#34;GET&#34;</span> <span style="color:#4e9a06">&#34;k1&#34;</span>        <span style="color:#8f5902;font-style:italic"># 由于k1 值发生变化，循环 &lt;4.1&gt; 会重新查询redis-server</span></code></pre></div>
<p>如上： 虽然是个死循环，但是关于 redis 操作只有以上注释的几条，说明客户端缓存生效。</p>

<h2 id="总结">总结</h2>

<ul>
<li>当前仅有 lettuce 支持此功能，jedis 还未支持</li>
<li>spring-boot-data-redis 暂未支持此功能，估计需要 spring boot 2.5 版本</li>
</ul>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis5.0%E7%89%88%E6%9C%AC%E4%BB%8B%E7%BB%8D/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis6.0-tls%E6%B5%8B%E8%AF%95/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>